package com.lemon.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * Title:工具类
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2011年5月13日
 * @版本 V 1.0
 */
public class StringUtil {
	private static Logger log = LoggerFactory.getLogger(StringUtil.class);

	/**
	 * Date格式的时间转成String类型
	 * 
	 * @Title: DateToString
	 * @param date 时间
	 * @param type 0:yyyy-MM-dd <br/>
	 *             1:yyyy/MM/dd <br/>
	 *             2:yyyyMMdd <br/>
	 *             21:yyyyMM <br/>
	 *             3:MM/dd/yy <br/>
	 *             4:yyyy-MM-dd HH:mm:ss <br/>
	 *             41:yyyy-MM-dd HH:mm:ss.SSS <br/>
	 *             5:yyyyMMddHHmm
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 * @mark 只提供了几种常用的.需要复杂的自行添加类型
	 */
	public static String DateToString(java.util.Date date , int type) {
		String result = null;
		String format = "yyyy-MM-dd";
		switch (type) {
		case 0:
			format = "yyyy-MM-dd";
			break;
		case 1:
			format = "yyyy/MM/dd";
			break;
		case 2:
			format = "yyyyMMdd";
			break;
		case 21:
			format = "yyyyMM";
			break;
		case 3:
			format = "MM/dd/yy";
			break;
		case 4:
			format = "yyyy-MM-dd HH:mm:ss";
			break;
		case 41:
			format = "yyyy-MM-dd HH:mm:ss.SSS";
			break;
		case 5:
			format = "yyyyMMddHHmm";
			break;
		default:
			format = "yyyy-MM-dd";
			break;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (date != null) {
			result = formatter.format(date);
		} else {
			result = "";
		}
		return result;
	}

	/**
	 * 给时间加上几个小时
	 * 
	 * @param day  当前时间 格式：yyyy-MM-dd HH:mm:ss
	 * @param hour 需要加的时间
	 * @return
	 */
	public static String addDateHour(String day , int hour) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR , hour);// 24小时制   
		date = cal.getTime();
		cal = null;
		return format.format(date);

	}

	/**
	 * 给时间加上几个小时
	 * 
	 * @param day  当前时间 格式：yyyy-MM-dd HH:mm:ss
	 * @param hour 需要加的时间
	 * @return
	 */
	public static Date addDateHour(Date day , int hour) {
		Date date = day;
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR , hour);// 24小时制   
		date = cal.getTime();
		cal = null;
		return date;

	}

	//给时间加几分钟
	public static Date addDateMinute(Date day , int minute) {
		Date date = day;
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE , minute);// 24小时制   
		date = cal.getTime();
		cal = null;
		return date;

	}
	/**
	 * <b>Description:<br>
	 * <em><blockquote>
	 * String格式的时间转成Date类型
	 * </blockquote></em></b>
	 * 
	 * @Title: StringToDate
	 * @param strDate 字符串时间
	 * @param type    0:yyyy-MM-dd <br/>
	 *                1:yyyy/MM/dd <br/>
	 *                2:yyyyMMdd <br/>
	 *                21:yyyyMM <br/>
	 *                3:MM/dd/yy <br/>
	 *                4:yyyy-MM-dd HH:mm:ss <br/>
	 *                41:yyyy-MM-dd HH:mm:ss.SSS <br/>
	 *                5:yyyyMMddHHmm
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 * @mark 只提供了几种常用的.需要复杂的自行添加类型
	 */
	public static java.util.Date StringToDate(String strDate , int type) {
		java.util.Date result = null;
		String format = "yyyy-MM-dd";

		switch (type) {
		case 0:
			format = "yyyy-MM-dd";
			break;
		case 10:
			format = "yyyy-MM";
			break;
		case 1:
			format = "yyyy/MM/dd";
			break;
		case 2:
			format = "yyyyMMdd";
			break;
		case 21:
			format = "yyyyMM";
			break;
		case 3:
			format = "MM/dd/yy";
			break;
		case 4:
			format = "yyyy-MM-dd HH:mm:ss";
			break;
		case 41:
			format = "yyyy-MM-dd HH:mm:ss.SSS";
			break;
		case 5:
			format = "yyyyMMddHHmm";
			break;
		default:
			format = "yyyy-MM-dd";
			break;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (strDate != null && !strDate.equals("")) {
			try {
				result = formatter.parse(strDate);
			} catch (ParseException ex) {
				result = null;
			}
		}
		return result;
	}

	/**
	 * 获取过去第几天的日期
	 * 
	 * @param past
	 * @return
	 */
	public static String getPastDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR , calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;
	}

	/**
	 * 获取过去第几天的日期
	 * 
	 * @param past
	 * @return
	 */
	public static String getPastDate(int past , String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR , calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");
		if (isNotBlank(format)) {
			formats = new SimpleDateFormat(format);
		}
		String result = formats.format(today);
		return result;
	}

	/**
	 * 获取未来 第 past 天的日期
	 * 
	 * @param past
	 * @return
	 */
	public static String getFetureDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR , calendar.get(Calendar.DAY_OF_YEAR) + past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;
	}


	/**
	 * 获取过去或者未来 任意天内的日期数组
	 * 
	 * @param intervals intervals天内
	 * @return 日期数组
	 */
	public static ArrayList<String> test(int intervals) {
		ArrayList<String> pastDaysList = new ArrayList<>();
		ArrayList<String> fetureDaysList = new ArrayList<>();
		for (int i = 0; i < intervals; i++) {
			pastDaysList.add(getPastDate(i));
			fetureDaysList.add(getFetureDate(i));
		}
		return pastDaysList;
	}

	/**
	 * @Title: getUUID
	 * @Description:(一句话阐述):获得一串英文数字混合的字符串
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 */
	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		return s.substring(0 , 8) + s.substring(9 , 13) + s.substring(14 , 18) + s.substring(19 , 23) + s.substring(24);
	}

	/**
	 * <b>Description:<blockquote> 获得一个区间范围的随机数, 包前包后
	 * </blockquote></b>
	 * 
	 * @Title: getRandomInMinMax
	 * @param MIN 最小值
	 * @param MAX 最大值
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月26日
	 */
	public static int getRandomInMinMax(Integer MIN , Integer MAX) {
		Random rand = new Random();
		int randNumber = rand.nextInt(MAX - MIN + 1) + MIN;
		return randNumber;
	}

	/**
	 * 判断字符串是否为空或null，为空返回true
	 *
	 * @param str
	 *            待判定字符串
	 * @return 是否为空或null
	 */
	public static boolean isEmpty(String str) {
		return (str == null || "".equals(str.trim()));
	}

	/**
	 * 判断参数是否为空，只有其中有一个参数为空则返回true，否则返回false
	 * 
	 * @author:zzy
	 * @date 2013-11-26上午10:42:34
	 * @modifier:
	 * @modify_description:
	 * @param objects
	 * @return
	 * @version v1.0.0
	 */
	public static boolean isEmpty(Object... objects) {
		boolean result = false;
		for (Object object : objects) {
			if (object instanceof String) {
				if (object == null || "".equals(object)) {
					return true;
				}
			}
			if (object instanceof Integer) {
				if (object == null || 0 == (Integer) object) {
					return true;
				}
			}
			if (object instanceof Double) {
				if (object == null || 0 == (Double) object) {
					return true;
				}
			}
			if (object instanceof Float) {
				if (object == null || 0 == (Float) object) {
					return true;
				}
			}
			if (object instanceof Long) {
				if (object == null || 0 == (Long) object) {
					return true;
				}
			}
			if (object instanceof Date) {
				if (object == null || null == (Date) object) {
					return true;
				}
			}
			if (object instanceof Timestamp) {
				if (object == null || null == (Timestamp) object) {
					return true;
				}
			}
			if (object instanceof Object) {
				if (object == null) {
					return true;
				}
			}
			if (object == null) {
				return true;
			}
		}
		return result;
	}

	/**
	 * 对象转为string格式，为空时，转为默认带入字符串
	 *
	 * @param obj
	 *                   待转对象
	 * @param defaultStr
	 *                   默认字符串
	 * @return 字符串
	 */
	public static String objToString(Object obj , String defaultStr) {
		if (obj == null) {
			return defaultStr;
		} else {
			return obj.toString().trim();
		}
	}

	/**
	 * 判断字符串长度是否合法
	 *
	 * @param instr
	 *                  输入字符串
	 * @param minLength
	 *                  最小长度
	 * @param maxLength
	 *                  最大长度
	 * @return true=合法
	 */
	public static boolean chackStringLength(String instr , int minLength , int maxLength) {
		return !(instr.length() < minLength || instr.length() > maxLength);
	}

	/**
	 * 判断字符串是否全是数字
	 *
	 * @param str
	 *            输入字符串
	 * @return true=全是数字
	 */
	public static boolean isNum(String str) {
		if (null == str) {
			return false;
		}
		return str.matches("\\d+");
	}

	/**
	 * 字符串长度处理，过长切除，不足补空格.
	 *
	 * @param str
	 *                   传入字符串
	 * @param length
	 *                   传入指定长度(字节数)
	 * @param defaultStr
	 *                   默认字符
	 * @param isRight
	 *                   是否右补齐
	 * @return 返回指定长度的字符串
	 */
	public static String supplyString(String str , int length , String defaultStr , boolean isRight) {
		if (str != null) {
			int len = str.length();
			if (len < length) {
				if (isRight) {
					for (int i = 0; i < (length - len); i++) {
						str = str + defaultStr;
					}
				} else {
					for (int i = 0; i < (length - len); i++) {
						str = defaultStr + str;
					}
				}
			} else {
				str = str.substring(0 , length);
			}
		} else {// 空字符串
			if (isRight) {
				for (int i = 0; i < length; i++) {
					str = str + defaultStr;
				}
			} else {
				for (int i = 0; i < length; i++) {
					str = defaultStr + str;
				}
			}
		}
		return str;
	}

	/**
	 * 字符串倒转
	 *
	 * @param str
	 *            输入字符串
	 * @return 倒转后字符串
	 */
	public static String reverseStr(String str) {
		StringBuilder strbuff = new StringBuilder();
		for (int i = str.length() - 1; i >= 0; i--) {
			strbuff.append(str.charAt(i));
		}
		return strbuff.toString();
	}

	/**
	 * @Title: createRand
	 * @Description:(一句话阐述):生成随机的几位数字 , 当第一位数字为0的时候重新生成第一位数字.
	 * @param k 要生成的数字的位数 . 例: k=2 生成12 k=5 生成 12345
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 */
	public static String createRand(int k) {
		Random random = new Random();
		String sRand = "";
		for (int i = 0; i < k; i++) {
			String rand = String.valueOf(random.nextInt(10));
			if (i == 0 && rand.equals("0")) {
				i--;
				continue;
			}
			sRand += rand;
		}
		return sRand;
	}

	public static String getHost(String host , String methodName) {
		StringBuilder resultUrl = new StringBuilder("");
		resultUrl.append(host);
		resultUrl.append("/" + methodName);
		resultUrl.append("/gzip");// 启用数据压缩
		return resultUrl.toString();
	}

	/**
	 * @Title: stringToMD5String
	 * @Description:(一句话阐述):MD5加密, 密文转换大写
	 * @param string 要加密的字符串
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 */
	public static String stringToMD5String(String string) {
		String md5 = MD5.getMD5(string);
		md5 = md5.toUpperCase();
		return md5;
	}

	public static Integer getInteger(String str) {
		Integer intTemp = 0;
		try {
			intTemp = Integer.valueOf(str);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return intTemp;
	}

	/**
	 * 获取本机IP
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<String> getHostIp() throws Exception {
		List<String> li = new ArrayList<String>();
		Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		while (allNetInterfaces.hasMoreElements()) {
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
			Enumeration addresses = netInterface.getInetAddresses();
			while (addresses.hasMoreElements()) {
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address) {
					System.out.println("本机的IP = " + ip.getHostAddress());
					li.add(ip.getHostAddress());
				}
			}
		}
		return li;
	}

	/**
	 * @Title: getParamString
	 * @Description:(一句话阐述):把map类型参数拼装成url参数连接的字符串格式 ;例: url?name=xx&id=xx
	 * @param params
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月13日
	 */
	public static String getParamStringToUrl(Map<String , String> params) {
		StringBuffer result = new StringBuffer("");
		if (params == null || params.isEmpty()) {
			return "";
		}
		Set<Map.Entry<String , String>> set = params.entrySet();
		Iterator<Map.Entry<String , String>> it = set.iterator();
		if (it.hasNext()) {
			Map.Entry<String , String> entry = (Map.Entry<String , String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append(param);
		}
		while (it.hasNext()) {
			Map.Entry<String , String> entry = (Map.Entry<String , String>) it.next();
			String param = entry.getKey() + "=" + entry.getValue();
			result.append("&" + param);
		}
		return result.toString();
	}

	/**
	 * 解析出url参数中的键值对
	 * 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
	 * 
	 * @param URL url地址
	 * @return url请求参数部分
	 */
	public static Map<String , String> getUrlParams(String URL) {
		Map<String , String> mapRequest = new HashMap<String , String>();
		String[] arrSplit = null;
		String strUrlParam = getUrlParamsString(URL);
		if (strUrlParam == null) {
			return mapRequest;
		}
		//每个键值为一组 www.2cto.com  
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			String[] arrSplitEqual = null;
			arrSplitEqual = strSplit.split("[=]");
			//解析出键值  
			if (arrSplitEqual.length > 1) {
				//正确解析  
				mapRequest.put(arrSplitEqual[0] , arrSplitEqual[1]);

			} else {
				if (!isEmpty(arrSplitEqual[0])) {
					//只有参数没有值，不加入  
					mapRequest.put(arrSplitEqual[0] , "");
				}
			}
		}
		return mapRequest;
	}

	/**
	 * 去掉url中的路径，留下请求参数部分
	 * 
	 * @param strURL url地址
	 * @return url请求参数部分
	 */
	private static String getUrlParamsString(String strURL) {
		String strAllParam = null;
		String[] arrSplit = null;
		strURL = strURL.trim();
		arrSplit = strURL.split("[?]");
		if (strURL.length() > 1) {
			if (arrSplit.length > 1) {
				if (arrSplit[1] != null) {
					strAllParam = arrSplit[1];
				}
			}
		}
		return strAllParam;
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 随即打乱list中的排序, 重新排序后返回 <br>
	 * Shuffle : 洗牌的意思
	 * </blockquote></em></b>
	 * 
	 * @Title: getShuffleList
	 * @param      <T>
	 * @param list
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月25日
	 */
	public static <T> List<T> getShuffleList(List<T> list) {
		Collections.shuffle(list);
		return list;
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 随即打乱list中的排序, 重新排序后返回 <br>
	 * Shuffle : 洗牌的意思
	 * </blockquote></em></b>
	 * 
	 * @Title: getShuffleList
	 * @param      <T>
	 * @param list
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月25日
	 */
	public static List getShuffleList2(List list) {
		Collections.shuffle(list);
		return list;
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 返回八大基本类型的封装类
	 * </blockquote></em></b>
	 * 
	 * @Title: checkOtherType
	 * @param type
	 * @param type2
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月4日
	 */
	public static String getParentType(String type) {
		switch (type) {
		case "int":
			type = "java.lang.Integer";
			break;
		case "byte":
			type = "java.lang.Byte";
			break;
		case "boolean":
			type = "java.lang.Boolean";
			break;
		case "short":
			type = "java.lang.Short";
			break;
		case "char":
			type = "java.lang.Character";
			break;
		case "long":
			type = "java.lang.Long";
			break;
		case "float":
			type = "java.lang.Float";
			break;
		case "double":
			type = "java.lang.Double";
			break;
		default:
			break;
		}

		return type;
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 参数值不为空 </blockquote></em></b>
	 * 
	 * @Title: isNotBlank
	 * @param sort
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月15日
	 */
	public static boolean isNotBlank(Object para) {
		boolean empty = isEmpty(para);
		return !empty;
	}

	public static String createUserNo(int len) {
		//随即生成uid
		String uids = "||00000000111111112222222233333333444444445555555566666666777777778888888899999999123456789";
		String uid = "";
		Random r = new Random();
		boolean flag = true;
		while (flag) {
			for (int i = 0; i < len; i++) {
				int rn = r.nextInt(10);
				//第一位数字不能为空
				if (i == 0 && rn == 0) {
					i--;
				} else {
					uid += rn;
				}
				//后五位不能为相同数字 和 连续数字
				if (i == (len - 1)) {
					int indexOf = uids.indexOf(uid.substring(1 , (len - 1)));
					if (indexOf > 0) {
						flag = true;
						break;
					}
				}
				flag = false;
			}
		}

		return uid;
	}

	/**
	 * <b>Description:<blockquote> 生成流水号 时间戳+5位随机数+用户编号
	 * </blockquote></b>
	 * 
	 * @Title: createSerialNo
	 * @param userNo
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月29日
	 */
	public static String createSerialNo(String userNo) {
		long timeMillis = System.currentTimeMillis();
		String createUserNo = createUserNo(6);
		return timeMillis + userNo + createUserNo;
	}

	/**
	 * <b>Description:<blockquote> 生成流水号 日期 + 时间戳+5位随机数
	 * </blockquote></b>
	 * 
	 * @Title: createSerialNo
	 * @param userNo
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月29日
	 */
	public static String createSerialNoCityRun() {
		String dateStr = DateToString(new Date() , 21);
		long timeMillis = System.currentTimeMillis();
		int minMax = getRandomInMinMax(10000 , 99999);
		String tt = timeMillis + "";
		tt = tt.substring(8 , tt.length());
		return dateStr + tt + minMax;
	}

	/**
	 * 返回单个字符串，若匹配到多个的话就返回第一个 , 包含匹配关键词返回
	 * 
	 * @param soap
	 * @param rgex
	 * @return 包含匹配关键词返回
	 */
	public static String getSubUtilSimple(String soap , String rgex) {
		Pattern pattern = Pattern.compile(rgex);// 匹配的模式  
		Matcher m = pattern.matcher(soap);
		while (m.find()) {
			return m.group();
		}
		return "";
	}

	/**
	 * date2比date1多的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDistanceDays(Date date1 , Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2)   //同一年
		{
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年            
				{
					timeDistance += 366;
				} else    //不是闰年
				{
					timeDistance += 365;
				}
			}
			return timeDistance + (day2 - day1);
		} else    //不同年
		{
			System.out.println("判断day2 - day1 : " + (day2 - day1));
			return day2 - day1;
		}
	}

	/**
	 * 两个时间之间相差距离多少天
	 * 
	 * @param one 时间参数 1：
	 * @param two 时间参数 2：
	 * @return 相差天数
	 */
	public static long getDistanceDays(String str1 , String str2) throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date one;
		Date two;
		long days = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			days = diff / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * 
	 * @param str1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return long[] 返回值为：{天, 时, 分, 秒}
	 */
	public static long[] getDistanceTimes(String str1 , String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long[] times = { day , hour , min , sec };
		return times;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * 
	 * @param str1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return String 返回值为：xx天xx小时xx分xx秒
	 */
	public static String getDistanceTime(String str1 , String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day + "天" + hour + "小时" + min + "分" + sec + "秒";
	}

	public static void main(String[] args) throws Exception {
		//		System.out.println(getDistanceDays("2016-11-01" , "2018-11-01"));
		System.out.println(getPastDate(1));
	}

}
